<?php
require_once '../includes/config.inc.php';

class MySQLDB
{
    var $db_conn;

    function connect_db() {
        $this->db_conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME);
        // 检测连接
        if ($this->db_conn->connect_error) {
            //error_log("连接失败: " . $conn->connect_error);
            return false;
        }
        return true;
    }

    function close_db() {
        $this->db_conn->close();
    }

    function inser_user($name, $password) {
        $sql = sprintf("INSERT INTO users (name, password)VALUES ('%s', '%s')", $name, $password);
        if ($this->db_conn->query($sql) === TRUE) {
            //echo "新记录插入成功";
            return true;
        } else {
            //echo "Error: " . $sql . "<br>" . $db_conn->error;
            return false;
        }
    }

    function check_user($name) {
        $sql = sprintf("SELECT * FROM users WHERE name='%s'", $name);
        $result = $this->db_conn->query($sql);
        if ($result->num_rows > 0) {
            return true;
        } else {
            return false;
        }
    }

    function check_password($name, $password) {
        $sql = sprintf("SELECT * FROM users WHERE name='%s' AND password='%s'", $name, $password);
        $result = $this->db_conn->query($sql);
        if ($result->num_rows > 0) {
            return true;
        } else {
            return false;
        }
    }
}

 ?>
